﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports BusinessRule.IBusiness
Imports DatabaseAccess.IDAL
Imports System.Data
Imports BusinessRule.Common
Imports DatabaseAccess.DALFactory
Imports External.APICalling
Imports BusinessRule.DTO

Public Class uMESCheckTempBusiness
    Implements uMESCheckTempIBusiness
    Private dal As IOracleDAL = uMESDalContext.instance.GetDalObject()

    ''' <summary>
    ''' 根据批次获取抽检信息
    ''' </summary>
    ''' <returns></returns>
    Private Function GetCheckTempInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESCheckTempIBusiness.GetCheckTempInfo
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strContainerName As String
            strContainerName = ""
            If QueryCondition.Keys.Contains("ContainerName") = True Then
                strContainerName = QueryCondition("ContainerName").ToString()
            End If

            strSQL = "SELECT Container.Containername, Productbase.Productname, Product.Description," & vbCrLf & _
                    "       Specbase.Specname, Spec.Specrevision, Container.Status, Container.Qty AS ContainerQty," & vbCrLf & _
                    "       CheckTempInfo.Qty, CheckTempInfo.Checkqty, CheckTempInfo.Eligibleqty," & vbCrLf & _
                    "       Employee.Fullname, CheckTempInfo.Submitdate, CheckTempInfo.CheckTempInfoName, CheckTempInfo.Notes" & vbCrLf & _
                    "FROM Container" & vbCrLf & _
                    "INNER JOIN Product ON Container.Productid = Product.Productid" & vbCrLf & _
                    "INNER JOIN Productbase ON Productbase.Productbaseid = Product.Productbaseid" & vbCrLf & _
                    "INNER JOIN Currentstatus ON Container.Currentstatusid = Currentstatus.Currentstatusid" & vbCrLf & _
                    "INNER JOIN Spec ON Currentstatus.Specid = Spec.Specid" & vbCrLf & _
                    "INNER JOIN Specbase ON Specbase.Specbaseid = Spec.Specbaseid" & vbCrLf & _
                    "LEFT JOIN CheckTempInfo ON CheckTempInfo.Containerid = Container.Containerid" & vbCrLf & _
                    "AND CheckTempInfo.Specid = Spec.Specid" & vbCrLf & _
                    "LEFT JOIN Employee ON Employee.Employeeid = CheckTempInfo.Submitemployeeid " & vbCrLf & _
                    "WHERE Container.ContainerName = '" & strContainerName & "'" & vbCrLf & _
                    "ORDER BY CheckTempInfo.Submitdate" & vbCrLf

            Dim dtMain As DataTable = dal.GetCheckTempInfo(strSQL)
            dtMain = DataAdjust.FormatDataSetSpecNameByColumName("SpecName", dtMain)
            Dim dtDetail As New DataTable
            dtDetail = dtMain.Clone
            If dtMain.Rows.Count > 0 Then
                For i = 0 To dtMain.Rows.Count - 1
                    If dtMain.Rows(i).IsNull("CheckTempInfoName") = False Then
                        Dim strCheckTempInfoName As String
                        strCheckTempInfoName = dtMain.Rows(i).Item("CheckTempInfoName")
                        If strCheckTempInfoName <> "" Then
                            Dim dr As DataRow
                            dr = dtDetail.NewRow
                            For j = 0 To dtMain.Columns.Count - 1
                                If dtMain.Rows(i).IsNull(j) = False Then
                                    dr(j) = dtMain.Rows(i).Item(j)
                                End If
                            Next
                            dtDetail.Rows.Add(dr)
                        End If
                    End If
                Next

            End If
            Dim ds As New DataSet
            ds.Tables.Add(dtMain)
            ds.Tables.Add(dtDetail)
            ds.Tables(0).TableName = "dtMain"
            ds.Tables(1).TableName = "dtDetail"

            oResult.DBDataSet = ds
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try


    End Function


    ''' <summary>
    ''' 创建抽检记录信息
    ''' </summary>
    ''' <param name="oCheckTempInfo">创建抽检记录所需的基础信息</param>
    ''' <param name="result">执行结果</param>
    ''' <param name="message">返回结果信息</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function CreateCheckTemp(ByVal oCheckTempInfo As CheckTempInfo, ByRef result As Boolean, ByRef message As String) As Boolean Implements uMESCheckTempIBusiness.CreateCheckTemp
        CreateCheckTemp = False
        result = False
        Try
            Dim strInfo As String = ""
            Dim success As Boolean = False
            Dim daServerHost As ServerHostInfo = oCheckTempInfo.ServerHostInfo

            Dim strTxnDocName, strTxnName, strEventName As String
            strTxnDocName = "CheckTempInfoMaintDoc"
            strTxnName = "CheckTempInfoMaint"
            strEventName = "New"

            Dim m_DataList As New List(Of ClientAPIEntity)

            Dim dataEntity As New ClientAPIEntity

            dataEntity.ClientDataTypeEnum = DataTypeEnum.DataField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "Name"
            dataEntity.ClientDataValue = oCheckTempInfo.CheckTempName
            m_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataTypeEnum = DataTypeEnum.DataField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "Notes"
            dataEntity.ClientDataValue = oCheckTempInfo.Notes
            m_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataTypeEnum = DataTypeEnum.ContainerField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "Container"
            dataEntity.ClientDataValue = oCheckTempInfo.ContainerName
            dataEntity.ClientOtherValue = ""
            m_DataList.Add(dataEntity)


            'dataEntity = New ClientAPIEntity
            'Dim strProdName As String = oCheckTempInfo.ProductName
            'Dim strProdRev As String = oCheckTempInfo.ProductRevision

            'dataEntity.ClientDataTypeEnum = DataTypeEnum.RevisionedObjectField
            'dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            'dataEntity.ClientDataName = "Product"
            'dataEntity.ClientDataValue = strProdName

            'If strProdRev <> "" Then
            '    dataEntity.ClientDataIsROR = False
            '    dataEntity.ClientDataVersion = strProdRev
            'Else
            '    dataEntity.ClientDataIsROR = True
            '    dataEntity.ClientDataVersion = strProdRev
            'End If
            'm_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            Dim strSpecName As String = oCheckTempInfo.SpecName
            Dim strSpecRev As String = oCheckTempInfo.SpecRevision

            dataEntity.ClientDataTypeEnum = DataTypeEnum.RevisionedObjectField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "Spec"
            dataEntity.ClientDataValue = strSpecName
            If strSpecRev <> "" Then
                dataEntity.ClientDataIsROR = False
                dataEntity.ClientDataVersion = strSpecRev
            Else
                dataEntity.ClientDataIsROR = True
                dataEntity.ClientDataVersion = strSpecRev
            End If
            m_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataTypeEnum = DataTypeEnum.DataField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "Qty"
            dataEntity.ClientDataValue = oCheckTempInfo.Qty
            m_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataTypeEnum = DataTypeEnum.DataField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "CheckQty"
            dataEntity.ClientDataValue = oCheckTempInfo.CheckQty
            m_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataTypeEnum = DataTypeEnum.DataField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "EligibleQty"
            dataEntity.ClientDataValue = oCheckTempInfo.EligibleQty
            m_DataList.Add(dataEntity)


            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataTypeEnum = DataTypeEnum.NamedObjectField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "SubmitEmployee"
            dataEntity.ClientDataValue = oCheckTempInfo.SubmitEmployee
            m_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataTypeEnum = DataTypeEnum.DataField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "SubmitDate"
            dataEntity.ClientDataValue = oCheckTempInfo.SubmitDate
            m_DataList.Add(dataEntity)

            'Dim serverHost As String = daServerHost.ServerHost
            'Dim serverPort As String = daServerHost.ServerPort
            Dim serverUser As String = daServerHost.UserName
            Dim serverPassword As String = daServerHost.Password

            Dim clientData As New CamstarClientAPI(serverUser, serverPassword)
            success = clientData.CreateModelByStandard(strTxnDocName, strTxnName, strEventName, m_DataList, strInfo)
            '创建批次结束

            If success = True Then
                CreateCheckTemp = True
                result = True
            End If

            message = strInfo

        Catch ex As Exception
            result = False
            message = ex.Message
            Return False
        End Try
    End Function




End Class

